package com.hllxd.hism.star.util;

import org.joda.time.DateTime;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * Created by BookThief on 2016/6/8.
 */
public class TimeUtil {
    public static Integer remainSecondOfDay() {
        return 86400 - DateTime.now().getSecondOfDay();
    }

    /**
     * 返回当前时间的时间戳,格式yyyyMMddHHmmss
     * @return
     */
    public static Long currentTimeHllDT() {
        DateTime now = DateTime.now();
        return currentTimeHllDT(now);
               
    }

    /**
     * 返回当前时间的时间戳,格式yyyyMMdd
     * @return
     */
    public static Long currentTimeHllDT8() {
        DateTime now = DateTime.now();
        return currentTimeHllDT8(now);
    }
    public static Long DT8(int nday) {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.add(Calendar.DATE, nday);
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
        return Long.valueOf(sdf1.format(calendar1.getTime()));
    }

    // 输入yyyyMMddHHmmss 返回对应时间戳（单位到s）
    // 20180301153801
//    public static final DateFormat HLL_STD = new SimpleDateFormat("yyyyMMddHHmmss");
//    public static final DateFormat HLL_STD_HH = new SimpleDateFormat("yyyyMMddHH");
    static {
//        HLL_STD.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
//        HLL_STD_HH.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
    }
//    public static Long toTs(String ds) {
//        try {
//            synchronized (HLL_STD) {
//                Date date = HLL_STD.parse(ds);
//                return date.getTime() / 1000;
//            }
//        } catch (ParseException e) {
//            return null;
//        }
//    }

    /**
     * 返回时间戳,格式yyyyMMddHHmmss
     * @param time joda时间
     * @return
     */
    public static Long currentTimeHllDT(DateTime time) {
        return time.getYear() * 10000000000L +
                time.getMonthOfYear() * 100000000L +
                time.getDayOfMonth() * 1000000L +
                time.getHourOfDay() * 10000L +
                time.getMinuteOfHour() * 100L +
                time.getSecondOfMinute();
    }

    /**
     * 返回时间戳,格式yyyyMMdd
     * @param time joda时间
     * @return
     */
    public static Long currentTimeHllDT8(DateTime time) {
        return time.getYear() * 10000L +
                time.getMonthOfYear() * 100L +
                time.getDayOfMonth() * 1L;
    }

    /**
     * 返回时间戳,格式yyyyMMddHHmmss
     * @param timestamp unix时间戳
     * @return
     */
    public static Long currentTimeHllDT(long timestamp) {
        DateTime time = new DateTime(timestamp);
        return currentTimeHllDT(time);
    }
    
    public static DateTime hllDT2DateTime(Long hllDT) {
        return new DateTime(
                (int)(hllDT/10000000000L),
                (int)((hllDT/100000000L)%100),
                (int)((hllDT/1000000L)%100),
                (int)((hllDT/10000L)%100),
                (int)((hllDT/100L)%100),
                (int)(hllDT%100)
        );
    }

    /**
     * 20160718121200 => '12:12'
     * @param hllDT
     * @return
     */
    public static String getFormattedTimeFromHllDT(Long hllDT) {
        Long time = hllDT%1000000;
        return String.format("%02d:%02d", time/10000, time%10000/100);
    }

    public static String getFormatted(Long hllDT) {
        Long time = hllDT%1000000;
        return String.format("%02d:%02d", time/10000, time%10000/100);
    }

    /**
     * 20160718121200 => 1212
     * @param hllDT
     * @return
     */
    public static Long getDigitalTimeFromHllDT(Long hllDT) {
        Long time = hllDT%1000000;
        return time/100;
    }

    /**
     * 20160718121200 => 20160718
     * @param hllDT
     * @return
     */
    public static Long getDigitalDateFromHllDT(Long hllDT) {
        Long date = hllDT/1000000;
        return date;
    }

    /**
     * 20160718121200 => 2016-07-18 12:12
     * @param hllDT
     * @return
     */
    public static String getDigitalDateTimeFromHllDT(Long hllDT) {
        return String.format("%04d-%02d-%02d %02d:%02d", hllDT / 10000000000L, hllDT % 10000000000L / 100000000,
            hllDT % 100000000 / 1000000, hllDT % 1000000 / 10000, hllDT % 10000 / 100);
    }

    /**
     * 返回当前年份yyyy
     * @return
     */
//    public static int getYear() {
//        DateTime now = DateTime.now();
//        return now.getYear();
//    }

    public static String time(String format,long ms) {
        Date date = new Date(ms);
        SimpleDateFormat df = new SimpleDateFormat(format);
        df.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        return df.format(date);
    }
    public static String time(long ms) {
        Date date = new Date(ms);
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        df.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        return df.format(date);
    }


    public static String dateFormat(Date date, String pattern) {
        DateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }
    public static Date dateParse(String date, String pattern) throws Exception {
        DateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.parse(date);
    }

    /**
     * 取得当前日期所在周的最后一天
     *
     * @param date
     * @return
     */
    public static Date getLastDayOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.SUNDAY);
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek() + 6); // Saturday
        return calendar.getTime();
    }

    public static String getDateBefore(Date date,int count,String pattern){
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, count);
        return dateFormat(cal.getTime(), pattern);
    }

    public static String getInputDateBefore(Date date,int count,String pattern){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, count);
        return dateFormat(cal.getTime(), pattern);
    }

    public static String getDayAfter(Date date,int count,String pattern){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int day = cal.get(Calendar.DATE);
        cal.set(Calendar.DATE, day + count);
        return dateFormat(cal.getTime(), pattern);
    }

    public static Date dateParse(String date) throws Exception {
        DateFormat df = DateFormat.getDateInstance();
        return df.parse(date);
    }

    public static String getWeekBegin(String dayID) {
        Date date;
        try {
            date = dateParse(dayID, "yyyyMMdd");
        }catch (Exception e){
            return null;
        }
        int mondayPlus;
        Calendar cd = Calendar.getInstance();
        cd.setTime(date);
        // 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) ;
        // 因为按中国礼拜一作为第一天所以这里减1
        if (dayOfWeek == 1) {
            //1是sunday
            mondayPlus = -6;
        } else {
            mondayPlus = 2 - dayOfWeek;
        }
        cd.add(Calendar.DATE, mondayPlus);
        Date monday = cd.getTime();
        return dateFormat(monday, "yyyyMMdd");
    }
    public static String getWeekEnd(String dayID) {
        Date date;
        try {
            date = dateParse(dayID, "yyyyMMdd");
        }catch (Exception e){
            return null;
        }
        int mondayPlus;
        Calendar cd = Calendar.getInstance();
        cd.setTime(date);
        // 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) ; // 因为按中国礼拜一作为第一天所以这里减1
        if (dayOfWeek == 1) {//1是sunday
            mondayPlus = 0;
        } else {
            mondayPlus = 7 + 1 - dayOfWeek;
        }
        //		GregorianCalendar currentDate = new GregorianCalendar();
        cd.add(Calendar.DATE, mondayPlus);
        Date monday = cd.getTime();
        return dateFormat(monday, "yyyyMMdd");
    }

    public static String getWeekBegin(int dayToAdd) {
        int mondayPlus;
        Calendar cd = Calendar.getInstance();
        cd.add(Calendar.DATE, dayToAdd);
        // 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) ; // 因为按中国礼拜一作为第一天所以这里减1
        if (dayOfWeek == 1) {//1是sunday
            mondayPlus = -6;
        } else {
            mondayPlus = 2 - dayOfWeek;
        }
        cd.add(Calendar.DATE, mondayPlus);
        Date monday = cd.getTime();
        return dateFormat(monday, "yyyyMMdd");
    }
    public static String getWeekEnd(int dayToAdd) {
        int mondayPlus;
        Calendar cd = Calendar.getInstance();
        cd.add(Calendar.DATE, dayToAdd);
        // 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) ; // 因为按中国礼拜一作为第一天所以这里减1
        if (dayOfWeek == 1) {//1是sunday
            mondayPlus = 0;
        } else {
            mondayPlus = 7 + 1 - dayOfWeek;
        }
        //		GregorianCalendar currentDate = new GregorianCalendar();
        cd.add(Calendar.DATE, mondayPlus);
        Date monday = cd.getTime();
        return dateFormat(monday, "yyyyMMdd");
    }
    /**
     * 得到某年某周的第一天
     *
     * @param year
     * @param week
     * @return
     */
    public static Date getFirstDayOfWeek(int year, int week) {
        week = week - 1;
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, Calendar.JANUARY);
        calendar.set(Calendar.DATE, 1);

        Calendar cal = (Calendar) calendar.clone();
        cal.add(Calendar.DATE, week * 7);

        return getFirstDayOfWeek(cal.getTime());
    }
    /**
     * 取得当前日期所在周的第一天
     *
     * @param date
     * @return
     */
    public static Date getFirstDayOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.SUNDAY);
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek()); // Sunday
        return calendar.getTime();
    }
    /**
     * 得到某年某周的最后一天
     *
     * @param year
     * @param week
     * @return
     */
    public static Date getLastDayOfWeek(int year, int week) {
        week = week - 1;
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, Calendar.JANUARY);
        calendar.set(Calendar.DATE, 1);
        Calendar cal = (Calendar) calendar.clone();
        cal.add(Calendar.DATE, week * 7);

        return getLastDayOfWeek(cal.getTime());
    }

//    public static Long toTime(String ds) {
//        try {
//            synchronized (HLL_STD) {
//                Date date = HLL_STD.parse(ds);
//                return date.getTime();
//            }
//        } catch (ParseException e) {
//            return null;
//        }
//    }

//    public static Long yyyyMMddHH2Ts(String ds) {
//        try {
//            synchronized (HLL_STD_HH) {
//                Date date = HLL_STD_HH.parse(ds);
//                return date.getTime();
//            }
//        } catch (ParseException e) {
//            return null;
//        }
//    }

    /**
     * 获取今天的日期
     * @return YYYYMMDD 示例：20190120
     */
    public static String getTodayDate(){
        Calendar cal=Calendar.getInstance();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        return format.format(cal.getTime());
    }

    public static void main(String[] args) {

        System.out.println(TimeUtil.currentTimeHllDT8());
//        System.out.println(TimeUtil.toTs("20180301120801"));
//        System.out.println(TimeUtil.toTs("20180301153801"));
        System.out.println(getWeekBegin(-4));
        System.out.println(getWeekEnd(-4));
        System.out.println(getWeekBegin(-3));
        System.out.println(getWeekEnd(-3));
        System.out.println(getWeekBegin(-5));
        System.out.println(getWeekEnd(-5));
        System.out.println(getWeekBegin(-24));
        System.out.println(getWeekEnd(-24));

        System.out.println(getWeekBegin("20180819"));
        System.out.println(getWeekEnd("20180819"));

        System.out.println(getWeekBegin("20180820"));
        System.out.println(getWeekEnd("20180820"));

        System.out.println(getWeekBegin("20180828"));
        System.out.println(getWeekEnd("20180828"));

        System.out.println(getWeekBegin("20180228"));
        System.out.println(getWeekEnd("20180228"));
        System.out.println(getTodayDate());
    }
}
